import { useCallback } from 'react';

export const useGameActions = (petState, setPetState, gameInfo, setGameInfo, taskSystemRef) => {
  const handleFeed = useCallback(() => {
    if (petState.hunger >= 100) return;
    
    setPetState(prev => ({
      ...prev,
      hunger: Math.min(prev.hunger + 20, 100),
      lastFed: Date.now(),
      health: prev.hunger < 20 ? Math.min(prev.health + 5, 100) : prev.health
    }));
    
    setGameInfo(prev => ({
      ...prev,
      coins: prev.coins + 5
    }));
    
    if (taskSystemRef.current) {
      taskSystemRef.current.updateFeedTask();
    }
  }, [petState, setPetState, setGameInfo, taskSystemRef]);

  const handlePlay = useCallback(() => {
    if (petState.energy < 20) return;
    
    setPetState(prev => ({
      ...prev,
      happiness: Math.min(prev.happiness + 15, 100),
      energy: Math.max(prev.energy - 10, 0),
      hunger: Math.max(prev.hunger - 5, 0),
      lastPlayed: Date.now()
    }));
    
    setGameInfo(prev => ({
      ...prev,
      coins: prev.coins + 10
    }));
    
    if (taskSystemRef.current) {
      taskSystemRef.current.updatePlayTask();
    }
  }, [petState, setPetState, setGameInfo, taskSystemRef]);

  const handleClean = useCallback(() => {
    setPetState(prev => ({
      ...prev,
      cleanliness: 100,
      lastCleaned: Date.now(),
      health: prev.cleanliness < 30 ? Math.min(prev.health + 10, 100) : prev.health
    }));
    
    setGameInfo(prev => ({
      ...prev,
      coins: prev.coins + 5
    }));
  }, [petState, setPetState, setGameInfo]);

  const handleSleep = useCallback(() => {
    setPetState(prev => ({
      ...prev,
      isSleeping: !prev.isSleeping,
      energy: prev.isSleeping ? prev.energy : Math.min(prev.energy + 30, 100)
    }));
  }, [petState, setPetState]);

  return {
    handleFeed,
    handlePlay,
    handleClean,
    handleSleep
  };
};